package com.cloud.login.service.impl;

import com.cloud.common.domain.Result;
import com.cloud.domain.LoginUser;
import com.cloud.entity.*;
import com.cloud.enums.AuthType;
import com.cloud.enums.LoginDevice;
import com.cloud.login.service.ILoginService;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LoginServiceImpl implements ILoginService {

    @Resource
    private AuthLoginService authLoginService;

    /**
     * PC端账户登录验证
     */
    @Override
    public Result userLogin(LoginBody loginBody, SysLoginLogs loginLogs) {
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = authLoginService.checkUserByLogin(loginBody, LoginDevice.PC.getCode());
        if (StringUtils.isNotNull(sysUser)) {
            loginLogs.setNickName(sysUser.getNickName());
        }
        loginUser.setUser(sysUser);
        loginUser.setSysCode(loginBody.getSysCode());
        // 添加登录日志
        loginLogs.setSysCode(loginBody.getSysCode());
        loginLogs.setLoginMethod(AuthType.METHOD_SYSTEM.getInfo());
        loginLogs.setUserName(loginBody.getUserName());
        loginLogs.setOperIp(ServletUtils.getClientIPAddress(ServletUtils.getRequest()));
        return authLoginService.saveLoginLogs(loginUser, loginLogs);
    }

    /**
     * 移动端账户登录验证
     */
    @Override
    public Result mobileUserLogin(LoginBody loginBody, SysLoginLogs loginLogs) {
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = authLoginService.checkUserByLogin(loginBody, LoginDevice.APP.getCode());
        if (StringUtils.isNotNull(sysUser)) {
            loginLogs.setNickName(sysUser.getNickName());
        }
        loginUser.setUser(sysUser);
        loginUser.setSysCode(loginBody.getSysCode());
        // // 添加登录日志
        loginLogs.setSysCode(loginBody.getSysCode());
        loginLogs.setLoginMethod(AuthType.METHOD_SYSTEM.getInfo());
        loginLogs.setUserName(loginBody.getUserName());
        loginLogs.setOperIp(ServletUtils.getClientIPAddress(ServletUtils.getRequest()));
        return authLoginService.saveLoginLogs(loginUser, loginLogs);
    }

    /**
     * 根据授权ID获取用户信息
     */
    @Override
    @Transactional
    public Result authLogin(AuthBody authBody, SysLoginLogs loginLogs) {
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = authLoginService.getUserByAuth(authBody);
        if (StringUtils.isNotNull(sysUser)) {
            loginUser.setUser(sysUser);
            loginUser.setSysCode(authBody.getSysCode());
            // 添加账户登录日志
            loginLogs.setUserName(sysUser.getUserName());
            loginLogs.setNickName(sysUser.getNickName());
            if (AuthType.METHOD_DING_DING.getCode().equals(authBody.getAuthType())) {
                loginLogs.setLoginMethod(AuthType.METHOD_DING_DING.getInfo());
            } else if (AuthType.METHOD_ZHEZHENG_NAIL.getCode().equals(authBody.getAuthType())) {
                loginLogs.setLoginMethod(AuthType.METHOD_ZHEZHENG_NAIL.getInfo());
            } else if (AuthType.METHOD_ZHEJIANG_OFFICE.getCode().equals(authBody.getAuthType())) {
                loginLogs.setLoginMethod(AuthType.METHOD_ZHEJIANG_OFFICE.getInfo());
            } else if (AuthType.METHOD_WECHAT.getCode().equals(authBody.getAuthType())) {
                loginLogs.setLoginMethod(AuthType.METHOD_WECHAT.getInfo());
            }
            loginLogs.setSysCode(authBody.getSysCode());
            loginLogs.setOperIp(ServletUtils.getClientIPAddress(ServletUtils.getRequest()));
            return authLoginService.saveLoginLogs(loginUser, loginLogs);
        } else {
            // 授权添加系统账户信息
            AuthUser authUser = authLoginService.saveAuthUserInfo(authBody);
            if (StringUtils.isNotNull(authUser)) {
                authBody.setAuthCode(authUser.getOpenid());
                return authLogin(authBody, loginLogs);
            } else {
                return Result.error("获取第三方应用授权信息失败");
            }
        }
    }

    /**
     * 设置用户/关联项目权限Token信息
     */
    @Override
    public Result setLoginPointToken(String sysCode) {
        return authLoginService.setLoginPointToken(sysCode);
    }

    /**
     * 根据授权Code获取Token信息
     */
    @Override
    public Result getAuthPointToken(String openId) {
        return authLoginService.getAuthPointToken(openId);
    }

}
